home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / Intros / IndexIntro / IndexIntro.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  2.6 KB  |  142 lines  |  [TEXT/CWIE]

  1. /*
  2. **    IndexIntro.c
  3. */
  4.  
  5. #include "agl.h"
  6.  
  7. #include <Fonts.h>
  8. #include <MacWindows.h>
  9. #include <math.h>
  10.  
  11. #define WIDTH  300
  12. #define HEIGHT 250
  13.  
  14. /*
  15. ** OpenGL Setup
  16. */
  17. static AGLContext setupAGL(AGLDrawable win)
  18. {
  19.     GLint          attrib[] = { AGL_NONE };
  20.     AGLPixelFormat fmt;
  21.     AGLContext     ctx;
  22.     GLboolean      ok;
  23.     int            i;
  24.  
  25.     /* Choose an rgb pixel format */
  26.     fmt = aglChoosePixelFormat(NULL, 0, attrib);
  27.     if(fmt == NULL) return NULL;
  28.  
  29.     /* Create an AGL context */
  30.     ctx = aglCreateContext(fmt, NULL);
  31.     if(ctx == NULL) return NULL;
  32.  
  33.     /* Attach the window to the context */
  34.     ok = aglSetDrawable(ctx, win);
  35.     if(!ok) return NULL;
  36.     
  37.     /* Make the context the current context */
  38.     ok = aglSetCurrentContext(ctx);
  39.     if(!ok) return NULL;
  40.  
  41.     /* Pixel format is no longer needed */
  42.     aglDestroyPixelFormat(fmt);
  43.     
  44.     /* Colormap tracking is disabled and a back buffer colormap is assigned
  45.     ** so that this intro will work with any graphics device depth. */
  46.     aglDisable(ctx, AGL_COLORMAP_TRACKING);
  47.  
  48.     /* Assign a simple colormap of RGB stripes */
  49.     for(i = 0; i < 256; i++)
  50.     {
  51.         GLint color[4];
  52.         color[0] = i;
  53.         color[1] = i%3 == 0 ? 65535 : 0;
  54.         color[2] = i%3 == 1 ? 65525 : 0;
  55.         color[3] = i%3 == 2 ? 65535 : 0;
  56.         aglSetInteger(ctx, AGL_COLORMAP_ENTRY, color);
  57.     }
  58.  
  59.     return ctx;
  60. }
  61.  
  62. /*
  63. ** OpenGL Cleanup
  64. */
  65. static void cleanupAGL(AGLContext ctx)
  66. {
  67.     aglSetCurrentContext(NULL);
  68.     aglSetDrawable(ctx, NULL);
  69.     aglDestroyContext(ctx);
  70. }
  71.  
  72. /*
  73. ** OpenGL Drawing
  74. */
  75. static void drawGL(void)
  76. {
  77.     int i;
  78.  
  79.     /* Clear color buffer to yellow */
  80.     glClearIndex(80.0f);
  81.     glClear(GL_COLOR_BUFFER_BIT);
  82.     
  83.     /* Disable dithering */
  84.     glDisable(GL_DITHER);
  85.     
  86.     /* Draw a triangle fan */
  87.     glBegin(GL_TRIANGLE_FAN);
  88.     glIndexf(0.0f);
  89.     glVertex2f( 0.0f, 0.0f);
  90.     for(i = 0; i <= 32; i++)
  91.     {
  92.         GLfloat f = i * 6.28319f / 32;
  93.         glIndexi(i);
  94.         glVertex2f(0.9f * cosf(f), 0.9f * sinf(f));
  95.     }
  96.     glEnd();
  97.  
  98.     /* Ensure completion */
  99.     glFinish();
  100. }
  101.  
  102. /*
  103. ** Macintosh main routine
  104. */
  105. int main(int argc, char *argv[])
  106. {
  107.     Rect       rect;
  108.     WindowPtr  win;
  109.     AGLContext ctx;
  110.     
  111.     /* Initialize Macintosh system */
  112.     InitGraf(&qd.thePort);
  113.     InitFonts();
  114.     InitWindows();
  115.  
  116.     /* Create a window */
  117.     SetRect(&rect, 50, 50, 50 + WIDTH, 50 + HEIGHT);
  118.     win = NewCWindow(NULL, &rect, "\pIndex Intro", false,
  119.                                 plainDBox, (WindowPtr) -1L, true, 0L);
  120.     if(win == NULL) return 1;
  121.     
  122.     /* Display the window */
  123.     ShowWindow(win);
  124.  
  125.     /* Setup the OpenGL context */
  126.     ctx = setupAGL((AGLDrawable) win);
  127.     if(!ctx) return 1;
  128.  
  129.     /* Do the OpenGL drawing */
  130.     drawGL();
  131.     
  132.     /* Wait until the mouse button is pressed */
  133.     while(!Button()) {}
  134.     
  135.     /* Cleanup the OpenGL context */
  136.     cleanupAGL(ctx);
  137.  
  138.     /* Cleanup */
  139.     DisposeWindow(win);
  140.  
  141.     return 0;
  142. }